package com.huadi.firematerialident.service;

import com.huadi.firematerialident.dao.NormalUserDaoImpl;
import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.Admin;
import com.huadi.firematerialident.po.NormalUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NormalUserServiceImpl implements NormalUserService{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NormalUserDaoImpl normalUserDao;
    @Autowired
    UserDaoImpl userDao;

    @Override
    public int addNormalUser(NormalUser normalUser) {
        return normalUserDao.addNormalUser(normalUser);
    }

    @Override
    public int deleteNormalUserByID(String nUserID) {
        return normalUserDao.deleteNormalUserByID(nUserID);
    }

    @Override
    public int updateNormalUser(NormalUser normalUser) {
        return normalUserDao.updateNormalUser(normalUser);
    }

    @Override
    public List<Map<String,Object>> selectNormalUserByID(String nUserID) {
        return normalUserDao.selectNormalUserByID(nUserID);
    }

    @Override
    public List<Map<String,Object>> getAllNormalUser() { return getAllNormalUser();
    }

    @Override
    public String getNUserID(){
        String NUserCount = userDao.getUsername();
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select NUserID,NUserCount from normaluser");
        for(int i=0;i<list.size();i++) {
            if (NUserCount.equals(list.get(i).get("NUserCount"))) {
                return (String) list.get(i).get("NUserID");
            }
        }
        return null;
    }

    @Override
    public NormalUser selectNormalUserByID_class(String id){
        List<Map<String ,Object>> list = normalUserDao.selectNormalUserByID(id);
        NormalUser normaluser = new NormalUser();
        normaluser.setNUserID((String) list.get(0).get("NUserID"));
        normaluser.setNUserCount((String) list.get(0).get("NUserCount"));
        normaluser.setNPassword((String) list.get(0).get("NUserPassword"));
        normaluser.setNName((String) list.get(0).get("NUserName"));
        normaluser.setNSex((String) list.get(0).get("NUserSex"));
        normaluser.setNBirth((LocalDate) list.get(0).get("NUserBirth"));
        normaluser.setNPhone((String) list.get(0).get("NUserPhone"));
        normaluser.setLogicalAddDel((boolean) list.get(0).get("LogicalAddDel"));
        normaluser.setLastModification((LocalDateTime) list.get(0).get("LastModification"));
        return normaluser;
    }
}
